<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
Compiling Procedures as Dynamic or Static
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot062.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot064.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc160">11.1</A>&nbsp;&nbsp;Compiling Procedures as Dynamic or Static</H2>
<A NAME="compdynamic"></A>
If it is intended that
a procedure be altered through the use of <A HREF="../bips/kernel/dynamic/assert-1.html"><B>assert/1</B></A><A NAME="@default624"></A> and <A HREF="../bips/kernel/dynamic/retract-1.html"><B>retract/1</B></A><A NAME="@default625"></A>,
<A NAME="@default626"></A>
<A NAME="@default627"></A>
the system should be informed that the procedure will be dynamic,
since these predicates are
designed to work on dynamic procedures. 
If <A HREF="../bips/kernel/dynamic/assert-1.html"><B>assert/1</B></A><A NAME="@default628"></A> is applied on a non-existing procedure, an error
is raised, however the default error handler for this error
only declares the procedure as dynamic and then makes the assertion.<BR>
<BR>
A procedure is by default static unless it has been specifically declared as
dynamic.
Clauses of static procedures must always be consecutive,
they may not
be separated in one or more source files or by the user from the top level.
If the static procedure clauses are not consecutive, each of the
consecutive parts is taken as a separate procedure which redefines
the previous occurrence of that procedure, and so only the last one will
remain.
However, whenever the compiler encounters nonconsecutive clauses of a static
procedure in one file, it raises an exception whose default handler
prints a warning but it continues to compile the rest of the file.<BR>
<BR>
If a procedure is to be dynamic the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> system should be
given a specific <I>dynamic declaration</I>
A dynamic declaration takes the form
<BLOCKQUOTE CLASS="quote"> <PRE CLASS="verbatim">
:- dynamic SpecList.
</PRE></BLOCKQUOTE>
<A NAME="@default629"></A>
The predicate <A HREF="../bips/kernel/dynamic/is_dynamic-1.html"><B>is_dynamic/1</B></A><A NAME="@default630"></A> may be used to check if a procedure
is dynamic:
<BLOCKQUOTE CLASS="quote"> <PRE CLASS="verbatim">
is_dynamic(Name/Arity).
</PRE></BLOCKQUOTE>
<A NAME="@default631"></A>
When the goal 
<BLOCKQUOTE CLASS="quote"> <PRE CLASS="verbatim">
compile(Somefile)
</PRE></BLOCKQUOTE>
is executed
and <TT>Somefile</TT> contains clauses for procedures that have
already been defined
in the Prolog database, those procedures are treated in one of two ways:
If such a procedure is dynamic, its clauses compiled from <TT>Somefile</TT>
are added to the database (just as would happen if they were asserted),
and the existing clauses are not affected.
For example, if the following
clauses have already been compiled:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
:- dynamic city/1.

city(london).
city(paris).
</PRE></BLOCKQUOTE>
and the file <TT>Somefile</TT> contains the
following Prolog code:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
city(munich).
city(tokyo).
</PRE></BLOCKQUOTE>
then compiling <TT>Somefile</TT> will cause adding
the clauses for <B>city/1</B> to those
already compiled, as <B>city/1</B> has been declared dynamic.
Thus the query <B>city(X</B>) will give:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
[eclipse 5]: city(X).
X = london    More? (;)

X = paris    More? (;)

X = munich    More? (;)

X = tokyo
yes.
</PRE></BLOCKQUOTE>
If, however, the compiled procedure is static,
the new clauses in <TT>Somefile</TT> replace the old procedure.
Thus, if
the following clauses have been compiled:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
city(london).
city(paris).
</PRE></BLOCKQUOTE>
and the file <TT>Somefile</TT> contains the following Prolog code:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
city(munich).
city(tokyo).
</PRE></BLOCKQUOTE>
when <TT>Somefile</TT> is compiled, then the procedure <B>city/1</B> is redefined.
Thus the query <TT>city(X</TT>) will give:
<BLOCKQUOTE CLASS="quote">
<PRE CLASS="verbatim">
[eclipse 5]: city(X).
X = munich    More? (;)

X = tokyo
yes.
</PRE></BLOCKQUOTE>
When the <A HREF="../bips/kernel/dynamic/dynamic-1.html"><B>dynamic/1</B></A><A NAME="@default632"></A> declaration is used on a procedure that is
already dynamic, which may happen for instance by recompiling a file
with this declaration inside, the system raises the error 64,
'procedure already dynamic'.
The default handler for this error, however, will only erase
all existing clauses for the specified procedure, so that
when such a file is recompiled several times during its debugging,
the system behaves as expected, the existing clauses
are always replaced.
The handler for this error can of course be changed if required.
If it is set to <A HREF="../bips/kernel/control/true-0.html"><B>true/0</B></A><A NAME="@default633"></A>, for instance,
the <A HREF="../bips/kernel/dynamic/dynamic-1.html"><B>dynamic/1</B></A><A NAME="@default634"></A> declaration is be just silently
accepted without erasing any clauses and without printing an error message.<BR>
<BR>
<HR>
<A HREF="umsroot062.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot064.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
